package com.system.service;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.system.dao.PermissionRepository;
import com.system.dao.RoleRepository;
import com.system.dto.PermissionDTO;
import com.system.entity.Permission;
import com.system.entity.Role;

@Service
@Transactional
public class PermissionService implements IPermissionService{

	@Autowired
	private PermissionRepository permissionRepository;
	@Autowired
	private RoleRepository roleRepository;
	
	@Override
	public void saveOrUpdate(Permission permission) {
		// TODO Auto-generated method stub
		permissionRepository.save(permission);
	}

	@Override
	public void delete(Long id) {
		// TODO Auto-generated method stub
		Permission permission=permissionRepository.findOne(id);
		List<Role> roles=roleRepository.findRoleByPermission(id);
		if(roles!=null) {
			for(Role role:roles) {
				role.getPermissionList().remove(permission);
				roleRepository.save(role);
			}
		}		
		permissionRepository.delete(id);
	}

	@Override
	public void delete(List<Long> ids) {
		// TODO Auto-generated method stub
		List<Permission> permissionList=(List<Permission>) permissionRepository.findAll(ids);
		if(permissionList!=null) {
			for(Permission permission:permissionList) {
				List<Role> roles=roleRepository.findRoleByPermission(permission.getId());
				if(roles!=null) {
					for(Role role:roles) {
						role.getPermissionList().remove(permission);
						roleRepository.save(role);
					}
				}
			}
			permissionRepository.delete(permissionList);
	    }
	}

	@Transactional(readOnly=true)
	@Override
	public Permission findOne(Long id) {
		// TODO Auto-generated method stub
		return permissionRepository.findOne(id);
	}

	@Transactional(readOnly=true)
	@Override
	public Page<PermissionDTO> findPage(Specification<Permission> spec, Pageable pageable) {
		// TODO Auto-generated method stub
		List<PermissionDTO> results = new ArrayList<PermissionDTO>();
		Page<Permission> page=permissionRepository.findAll(spec,pageable);
		for(Permission permission:page.getContent()) {
			PermissionDTO permissionDTO = new PermissionDTO();
        	BeanUtils.copyProperties(permission, permissionDTO);
        	
        	results.add(permissionDTO);
		}
		System.out.println("TotalElements"+page.getTotalElements());
		System.out.println("getSize"+page.getSize());
		System.out.println("getNumberOfElements"+page.getNumberOfElements());
		return new PageImpl<PermissionDTO>(results, pageable, page.getTotalElements());
	}
	
	

	@Transactional(readOnly=true)
	@Override
	public List<Permission> findAll() {
		// TODO Auto-generated method stub
		return (List<Permission>) permissionRepository.findAll();
	}

	@Override
	public Set<String> getTheUrl(String staffNum) {
		// TODO Auto-generated method stub
		List<Role>rolelist= roleRepository.findRoleByStaffNum(staffNum);
		Set<String> set=new HashSet<String>();
		for(Role role:rolelist) {
			for(Permission permission:role.getPermissionList()) {
				set.add(permission.getPermission());
			}
		}
		
		return set;
	}

}
